/*******************************************************************************
 * Copyright (c) 2008-2009 zhang yuexiang.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package org.xfeep.alaya.jta.test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

import org.xfeep.asura.core.CoreConsts;
import org.xfeep.asura.core.annotation.Activate;
import org.xfeep.asura.core.annotation.Config;
import org.xfeep.asura.core.annotation.Property;
import org.xfeep.asura.core.annotation.Ref;
import org.xfeep.asura.core.annotation.Service;

@Service
@TransactionManagement(TransactionManagementType.CONTAINER)
@Config("SmartConnectionTestClient")
public class SmartConnectionTestClient {
	
	@Ref(matcher="dataSourceConfigId=$.dataSourceConfigId" + CoreConsts.ON_DEMAND_SEPRATOR)
	Connection conn;
	
	@Property
	String dataSourceConfigId;
	
	public void i1() throws SQLException {
		Statement stmt = conn.createStatement();
		stmt.execute("insert into TestClient(id) values('i1')");
		stmt.close();
	}
	
	
	public void i2f() throws SQLException {
		System.out.println("start i2f()");
		Statement stmt = conn.createStatement();
		stmt.execute("insert into TestClient(id) values('i2f')");
		stmt.close();
		throw new SQLException("we just throw and test i2()");
	}
	
	public void i3f() throws SQLException {
		Statement stmt = conn.createStatement();
		stmt.execute("insert into TestClient(id) values('i3f')");
		stmt.close();
		i1();//this will throw exception for duplicate primary key;
	}
	
	public void i4() throws SQLException {
		Statement stmt = conn.createStatement();
		stmt.execute("insert into TestClient(id) values('i4')");
		stmt.close();
		i42();
	}
	
	private void i42() throws SQLException {
		Statement stmt = conn.createStatement();
		stmt.execute("insert into TestClient(id) values('i42')");
		stmt.close();
	}
	
	public void i5f() throws SQLException {
		Statement stmt = conn.createStatement();
		stmt.execute("insert into TestClient(id) values('i5f')");
		stmt.close();
		i5f2();
	}
	
	public String find(String id) throws SQLException{
		Statement stmt = null;
		ResultSet rs = null;
		try{
			stmt = conn.createStatement();
			rs = stmt.executeQuery("select id from TestClient where id = '" + id + "'");
			if (rs.next()){
				return rs.getString(1);
			}
			return null;
		}finally{
			if (rs != null){
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (stmt != null){
				try {
					stmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}
	
	private void i5f2() throws SQLException {
		throw new RuntimeException("we just throw test in i5f2()");
	}
	
	@Activate
	public void start() throws SQLException {
		Statement stmt = conn.createStatement();
		try{
			stmt.execute("drop table TestClient");
		}catch (Exception e) {
			System.err.println(e.getMessage());
		}
		stmt.execute("create table TestClient(id varchar(32) primary key)");
		stmt.close();
	}
	
}
